<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
use EasyWeChat\Factory;
use think\Config;

class TemplateController extends RestBaseController
{

    public $Token;
    public $UserId;
    public function __construct(){
        $token =  input('token') ? input('token') : $_SERVER['HTTP_TOKEN'];
        if(!$token){
            $this->error('请先登陆！');
        }
        $session = Db::table('a_user')->where(['session'=>$token])->value('session');
        if(!$session){
            echo json_encode(array('code'=>'90001','msg'=>'token已过期'));die;
            $this->error('token已过期!');
        }
        $this->Token = $token;
        $this->UserId = substr($this->Token,12);
    }

    /**
     * [template 模板信息]
     * @return [type] [description]
     */
    public function template(){
        $template = Db::table('minipro_template')->where(['type_'=>1])->select();
        if(!$template){
            $this->success('暂无模版!',array());
        }
        $utemplate = array();
        foreach ($template as $key => $value) {
            $utemplate[$key]['id'] = $value['template_id'];
            $utemplate[$key]['description'] = $value['description'];
            $utemplate[$key]['name'] = $value['name'];
            $utemplate[$key]['show_photo_url'] = $value['show_photo_url'];
            $utemplate[$key]['example_url'] = $value['example_url'];
            //$utemplate[$key]['template_info'] = utf8_encode($value['template_info']);
        }
        $this->success('请求成功！',$utemplate);
    }

    /**
     * [addTemplate 模版添加]
    */
    public function addWechXcx(){
        if(!input('template_id')){
            $this->error('缺少模板ID！');  
        }
        $biz_id = Db::table('minipro_copy')->insertGetId([
            'merchant_id'=> $this->UserId,
            'template_id'=> input('template_id'),
            'create_time'=> date('Y-m-d H:i:s')
        ]);
        if($biz_id){
            $this->success('添加成功',$biz_id);
        }
        $this->error('添加失败!');
    }

    /**
     * [userInfo 绑定的小程序列表]
     * @return [type] [description]
     */
    public function wechXcxlist(){
        $data = Db::prefix_close()->table('minipro_copy')
            ->alias('a')
            ->join('minipro_template b','b.template_id=a.template_id','left')
            ->field('a.biz_id,a.app_name,a.datainfo,a.img_log,a.status,b.template_id,b.name')
            ->where(['a.merchant_id'=>$this->UserId])
            ->order('a.biz_id desc')
            ->select();
        if(!$data){
            $this->success('无绑定小程序',array());
        }
        $this->success('请求成功！',$data);
    }

    /**
     * [gettem 获取微信模版]
     * @return [type] [description]
     */
    public function gettem(){
        $access_token = getAuthorizerAccessToken($this->UserId);
        $url = 'https://api.weixin.qq.com/wxa/gettemplatelit?access_token='.$access_token;
        $rest = post($url,'');
        $rest_list = json_decode($rest,true);
        print_r($rest_list);die;
        return $rest_list;
    }

    /**
     * [getUserinfo 获取登陆用户头像昵称]
     * @return [type] [description]
     */
    public function getUserinfo(){
        $data = Db::table('a_user')
            ->where(['id'=>$this->UserId])
            ->field('nickName,headerimg')
            ->find();
        if(!$data){
            $this->success('请求成功！',$data);
        }
        $this->error('网络错误！');
    }
}